package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.modellib.model.SheetModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 漫单推荐适配器
 * Created by Administrator on 2017/10/10.
 */

public class HomeSheetAdapter extends RecyclerView.Adapter<HomeSheetAdapter.ViewHolder> {

    private List<IModel.ISheetModel> mListSheet;
    private Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mPic;  //图片
        private TextView mTime; //发布时间
        private TextView mCollectCount; //阅读人数
        private TextView mAnimeTitle; //动漫标题
        private TextView mUserName;//副标题
        private View mSheetView;  //view

        public ViewHolder(View itemView) {
            super(itemView);
            mPic = (ImageView) itemView.findViewById(R.id.mIvPic);
            mTime = (TextView) itemView.findViewById(R.id.mTvTime);
            mCollectCount = (TextView) itemView.findViewById(R.id.mTvCollectCount);
            mAnimeTitle = (TextView) itemView.findViewById(R.id.mTvAnimeTitle);
            mUserName = (TextView) itemView.findViewById(R.id.mTvUserName);
            mSheetView = itemView;
        }
    }

    public HomeSheetAdapter(List<IModel.ISheetModel> mSheet, Context context) {
        this.mListSheet = mSheet;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_diffues_single_recommends, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final SheetModel sheetModel = (SheetModel) mListSheet.get(position);

        Glide.with(mContext)
                .load(sheetModel.getMandanImg())
                .into(holder.mPic);
        holder.mTime.setText(sheetModel.getCreateTime());
        holder.mCollectCount.setText(String.valueOf(sheetModel.getAnimeCount()));
        holder.mAnimeTitle.setText(sheetModel.getMandanTitle());
        holder.mUserName.setText(sheetModel.getUser().getUserName());

        //单击事件
        holder.mSheetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof Activity) {
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_SHEET_INFO());
                    intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), sheetModel.getMandanId());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                } else {
                    throw new RuntimeException(mContext.toString() + "必须是Activity!");
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return mListSheet.size();
    }


}
